/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.dongwu.common.dao;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.Predicate;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Range;
import org.springframework.data.jpa.domain.Specification;

import com.dongwu.common.utils.Collections3;
import com.github.wenhao.jpa.PredicateBuilder;
import com.github.wenhao.jpa.Specifications;
import com.google.common.collect.Lists;

public class DynamicSpecifications {
	
	public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters, final Class<T> entityClazz) {
		SimpleDateFormat sdf =   new SimpleDateFormat( "yyyy-MM-dd" );
		  PredicateBuilder builder= Specifications.<T>and();
		  if (Collections3.isNotEmpty(filters)) {

				List<Predicate> predicates = Lists.newArrayList();
				for (SearchFilter filter : filters) {
					// nested path translate, 如Task的名为"user.name"的filedName, 转换为Task.user.name属性
					String[] names = StringUtils.split(filter.fieldName, ".");
//					Path expression = root.get(names[0]);
//					for (int i = 1; i < names.length; i++) {
//						expression = expression.get(names[i]);
//					}

					// logic operator
					switch (filter.operator) {
					case EQ:
						builder.eq(filter.fieldName, filter.value);
						break;
					case LIKE:
						builder.like(filter.fieldName, "%"+filter.value.toString()+"%");
						
						break;
						
					case DBTW:
						String[] values=filter.value.toString().split("&");
						try {
						Date start=sdf.parse("1970-01-01");
						Date end = new Date();
						
						if(!StringUtils.isEmpty(values[0])){
							start=sdf.parse(values[0]);
						}
						if(values.length>1&&!StringUtils.isEmpty(values[1])){
							end=sdf.parse(values[1]);
						}
						
						builder.between(filter.fieldName, new Range<>(start, end));	
						} catch (ParseException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						
//						predicates.add(builder.greaterThan(expression, (Comparable) filter.value));
						break;
						
					case IN: 
						String[] vs= filter.value.toString().split(",");
						builder.in(filter.fieldName,vs); //or in("name", "Jack", "Eric")	
					    break;
					case NOTIN: 
						String[] notinvs= filter.value.toString().split(",");
						builder.notIn(filter.fieldName,notinvs); //or notIn("name", "Jack", "Eric")	
					    break;
					    
					case GT:
//						builder.between(property, range)
//						predicates.add(builder.greaterThan(expression, (Comparable) filter.value));
						break;
					case LT:
//						predicates.add(builder.lessThan(expression, (Comparable) filter.value));
						break;
					case GTE:
//						predicates.add(builder.greaterThanOrEqualTo(expression, (Comparable) filter.value));
						break;
					case LTE:
//						predicates.add(builder.lessThanOrEqualTo(expression, (Comparable) filter.value));
						break;
					}
				}
//
//				// 将所有条件用 and 联合起来
//				if (!predicates.isEmpty()) {
//					return builder.and(predicates.toArray(new Predicate[predicates.size()]));
//				}
			}
		
		return builder.build();
	}	
		
}
